import sys
from os import name
from os import system
from os import getcwd
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
from video_compressor.read_config import ReadConfig


if name == 'nt':  # 仅Windows
    # 设置控制台窗口标题
    system('视频处理')
    # 设置控制台窗口大小（列x行）
    system('mode con: cols=200 lines=40')


class Colors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'


def printf(msg, end='\n'):
    sys.stdout.write(msg+end)
    sys.stdout.flush()


def get_file(dir_path: Path):
    for file_path in dir_path.iterdir():
        if file_path.is_file(): yield file_path
        else:
            for _ in get_file(file_path): yield _


def split_duration_by_count(duration, num_segments):
    segment_length = duration / num_segments
    for i in range(num_segments):
        yield (int(round(i * segment_length, 3)), int(round((i + 1) * segment_length, 3)))


BASE_DIR = Path(getcwd())
CONFIG_FILE_PATH = BASE_DIR / 'config.ini'
config = ReadConfig(CONFIG_FILE_PATH)
VIDEO_WIDE = int(config.config['BASE']['video_wide']) if config.config['BASE'].get('video_wide') else 0
VIDEO_HIGH = int(config.config['BASE']['video_high']) if config.config['BASE'].get('video_high') else 0
GROUP_NUM = config.config.getint('BASE', 'group_num', fallback=5)
MIX_TIME = config.config.getfloat('BASE', 'min_time', fallback=180.0)
MAX_FRAMES = config.config.getint('BASE', 'max_frames', fallback=90)
MAX_WORKERS = config.config.getint('BASE', 'max_workers', fallback=2)
IN_DIR_PATH = Path(config.config.get('DIR_PATH', 'in_dir', fallback=BASE_DIR.as_posix()))
OUT_DIR_PATH = Path(config.config.get('DIR_PATH', 'out_dir', fallback=BASE_DIR.as_posix()))
VIDEO_EXTS = tuple([item.strip() for item in config.config['EXTS']['video'].split(",")])
EXECUTABLE = config.config.get('FFMPEG', 'path', fallback='ffmpeg')
LOGLEVEL = config.config.get('FFMPEG', 'loglevel', fallback='error')
HWACCEL = config.config.get('FFMPEG', 'hwaccel', fallback='')
THREADS = config.config.get('FFMPEG', 'threads', fallback='4')
C_V = config.config.get('FFMPEG', 'c_v', fallback='h264_nvenc')
B_V = config.config.get('FFMPEG', 'b_v', fallback='2000k')
MINRATE = config.config.get('FFMPEG', 'minrate', fallback='2000k')
BUFSIZE = config.config.get('FFMPEG', 'bufsize', fallback='2000k')
C_A = config.config.get('FFMPEG', 'c_a', fallback='aac')
B_A = config.config.get('FFMPEG', 'b_a', fallback='128k')
AR = config.config.get('FFMPEG', 'ar', fallback='44100')
RC = config.config.get('FFMPEG', 'rc', fallback='vbr')
PRESET = config.config.get('FFMPEG', 'preset', fallback='fast')
MOVFLAGS = config.config.get('FFMPEG', 'movflags', fallback='+faststart')
MIN_FILE = config.config.getint('FFMPEG', 'min_file', fallback=0)
MAX_FILE = config.config.getint('FFMPEG', 'max_file', fallback=10)
TRANSITION = config.config.get('FFMPEG', 'transition', fallback='slideleft')
THREAD_POOL = ThreadPoolExecutor(max_workers=MAX_WORKERS, thread_name_prefix=f'video_compressor_')

dir_list = [IN_DIR_PATH, OUT_DIR_PATH]
for d in filter(lambda f: not f.is_dir(), dir_list): d.mkdir(parents=True, exist_ok=True)

